package com.casic.ops.entity.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author mianbin
 */
@Data
@NoArgsConstructor
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)/*空不参与序列化*/
public class OpsResult<E> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * "状态 true：操作成功  false：操作失败"
     */
    Boolean state = true;

    String message;

    Long code;

    E value;

    Long total;

    Long current;

    Long Size;

    private String errorCode;

    public OpsResult(boolean state) {
        this(state, null, null);
    }

    public OpsResult(String message) {
        this(true, message, null);
    }

    /**
     * 返回成功/失败，及对应的成功/失败提示信息
     *
     * @param state
     * @param message
     */
    public OpsResult(boolean state, String message) {
        this(state, message, null);
    }

    /**
     * 返回成功/失败，及对应的成功/失败提示信息，还有返回对应的数据
     *
     * @param state
     * @param message
     * @param value
     */
    public OpsResult(boolean state, String message, E value) {
        this.state = state;
        this.message = message;
        this.value = value;
    }

    /**
     * 返回错误，错误编码，对应的错误信息
     *
     * @param errorCode
     * @param message
     */
    public OpsResult(String errorCode, String message) {
        this.state = false;
        this.errorCode = errorCode;
        this.message = message;
    }

    /**
     * 返回错误，错误编码，对应的错误信息
     *
     * @param errorCode
     * @param message
     */
    public OpsResult(boolean state, String message, E value , Long code) {
        this.state = false;
        this.errorCode = errorCode;
        this.message = message;
        this.code = code;
    }

    /**
     * 分页加信息
     *
     * @param value
     * @param total
     * @param current
     * @param size
     * @param message
     */
    public OpsResult(String message, E value, Long total, Long current, Long size) {
        this.message = message;
        this.value = value;
        this.total = total;
        this.current = current;
        Size = size;
    }

    /**
     * 分页
     *
     * @param value
     * @param total
     * @param current
     * @param size
     */
    public OpsResult(E value, Long total, Long current, Long size) {
        this.value = value;
        this.total = total;
        this.current = current;
        Size = size;
    }

    public static <E> OpsResult<E> createBySuccess() {
        return new OpsResult<E>(true);
    }

    public static <E> OpsResult<E> createBySuccessMessage(String msg) {
        return new OpsResult<E>(true, msg);
    }

    public static <E> OpsResult<E> createBySuccess(E data) {
        return new OpsResult<E>(true, null, data);
    }

    public static <E> OpsResult<E> createBySuccess(String msg, E data) {
        return new OpsResult<E>(true, msg, data);
    }

    public static <E> OpsResult<E> createBySuccessAndCode(String msg, E data , Long code) {
        return new OpsResult<E>(true, msg, data , code);
    }

    public static <E> OpsResult<E> createByErrorMessage(String errorMessage) {
        return new OpsResult<E>(false, errorMessage);
    }

    public static <E> OpsResult<E> createByPageMsg(String msg, E e, long total, Long current, Long size) {
        return new OpsResult<>(msg, e, total, current, size);
    }

    public static <E> OpsResult<E> createByPage(E e, long total, Long current, Long size) {
        return new OpsResult<>(e, total, current, size);
    }

    /*这种写法真low , 快点搞,后面改,这个要改成动态代理转换的方式 todo*/
    public static <E> OpsResult<E> createByInPage(E e, IPage page) {
        return new OpsResult<>(e, page.getTotal(), page.getCurrent(), page.getSize());
    }

}
